package cn.justweb;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.concurrent.locks.ReentrantLock;


public class AutoReplace {

    public static void main(String[] args) throws Exception{


        String path = "C:\\Users\\yushanbo\\Desktop\\简维JAVAEE课件\\2.阶段二-JavaWeb";

        getAllFile(new File(path));

        new ReentrantLock(true);


    }



    public static void getAllFile(File file) throws Exception{
        if (file.exists()) {
            //判断文件是否是文件夹，如果是，开始递归
            if (file.isDirectory()) {
                File f[] = file.listFiles();
                for (File file2 : f) {
                    getAllFile(file2);
                }
            } else {
               if(file.getName().endsWith(".md")||file.getName().endsWith(".html")||file.getName().endsWith(".css")||file.getName().endsWith(".jsp")||file.getName().endsWith(".txt")){

                   String content = FileUtils.readFileToString(file, "UTF-8");

                   String replaceString = content.replace("com.itheima", "cn.justweb").replace("cn.itcast","cn.justweb").replace("黑马","justweb").replace("itcast","justweb").replace("itheima","justweb").replace("heima","justweb").replace("czbk","justweb").replace("jinyanlong ","njit").replace("czbk ","njit").replace("传智播客 ","justweb").replace("传智博客 ","justweb");
                   replaceString = replaceString.replace("HEIMA","JUSTWEB");
                   replaceString = replaceString.replace("Heima","Justweb");
                   replaceString = replaceString.replace("HeiMa","JustWeb");
                   replaceString = replaceString.replace("Itcast","Justweb");
                   replaceString = replaceString.replace("ITCAST","JUSTWEB");


                   FileUtils.writeStringToFile(file,replaceString,"UTF-8");

               }else if(file.getName().endsWith(".png")||file.getName().endsWith(".jpg")){

                   String replaceName = file.getName().replace("com.itheima", "cn.justweb").replace("cn.itcast","cn.justweb").replace("黑马","justweb").replace("itcast","justweb").replace("itheima","justweb").replace("heima","justweb");

                   String absolutePath = file.getAbsolutePath();

                   String replaceFileName = StringUtils.substringBeforeLast(absolutePath,"\\") +"\\" + replaceName;

                   System.out.println("replaceFileName = " + replaceFileName);


                   file.renameTo(new File(replaceFileName));

               }
            }
        }
    }
}
